''' PyQt4闹钟+倒计时example.包括了几个控件的基本访问方法,
以及QTimer,QTime两个重要的类,不要试图用其他库开启新线程,用QTimer是正道,
否则无法与PyQt4协调'''

import clockcountdown_ui

from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainWindow,self).__init__()
        self.ui = clockcountdown_ui.Ui_MainWindow()
        self.ui.setupUi(self)
        # 以上3句就完成了界面设定.注意下面还需要实现信号处理
        self.alarm_timer = QtCore.QTimer()
        self.countdown_timer = QtCore.QTimer()
        self.alarm_timer.setSingleShot(False)   # 设置到时信号是否只生效一次
        self.countdown_timer.setSingleShot(False)
        self.alarm_set = (-1,-1,-1)
        self.countdown_sec = -1
        self.clock_timer=QtCore.QTimer()
        self.clock_timer.setSingleShot(False)
        self.clock_timer.start(1000)    # 开始timer,单位毫秒
        current = QtCore.QTime.currentTime()
        self.ui.alarmhour_sb.setValue(current.hour())   # 读写组件的字面一般都是setText(string),setValue(value),text(),value()
        self.ui.alarmminute_sb.setValue(current.minute())
        self.ui.alarmsecond_sb.setValue(current.second())

        # designer生成信号连接的方法
        QtCore.QObject.connect(self.alarm_timer,QtCore.SIGNAL("timeout()"),self.alarm_timeout)
        QtCore.QObject.connect(self.countdown_timer,QtCore.SIGNAL("timeout()"),self.countdown_timeout)
        QtCore.QObject.connect(self.clock_timer,QtCore.SIGNAL("timeout()"),self.clock_timeout)
        return

    def alarm_click(self):
        if self.alarm_timer.isActive():
            self.alarm_timer.stop()
            self.ui.statusbar.showMessage('闹钟已终止') # 改变状态栏的方法
            return False 
        else:
            self.alarm_set = (self.ui.alarmhour_sb.value(),self.ui.alarmminute_sb.value(),self.ui.alarmsecond_sb.value())
            self.alarm_timer.start(500)
            self.ui.statusbar.showMessage('开启闹钟,设定为' + str(self.alarm_set[0]) + '时' + str(self.alarm_set[1]) + '分' + str(self.alarm_set[2]) + '秒')
            return True
        return

    def countdown_click(self):
        if self.countdown_timer.isActive():
            self.countdown_timer.stop()
            self.ui.statusbar.showMessage('倒数已终止')
            return False
        else:
            self.countdown_timer.start(1000)
            self.countdown_sec = int(self.ui.comboBox.currentText())
            self.ui.statusbar.showMessage('倒数开始')
            return True
        return

    def clock_timeout(self):
        current = QtCore.QTime.currentTime()
        s = str(current.hour()) + ':' + str(current.minute()) + ':' +str(current.second())
        self.ui.time_lb.setText(s)
        return

    def alarm_timeout(self):
        current = QtCore.QTime.currentTime()
        ct = (current.hour(),current.minute(),current.second())
        if ct == self.alarm_set:
            QtGui.QWidget.activateWindow(self)
            QtGui.QMessageBox.information(self,'alarm time up',self.ui.alarmmessage_le.text())
            self.alarm_timer.stop()
        return

    # 注意必须用QtGui.QWidget.activateWindow(self)才能让窗口前置
    def countdown_timeout(self):
        self.ui.countdownshow_lb.setText(str(self.countdown_sec))
        if self.countdown_sec < 1:
            QtGui.QWidget.activateWindow(self)
            # 弹出对话框的方法
            QtGui.QMessageBox.information(self,'countdown time up','倒计时结束')

            self.countdown_timer.stop()
            self.ui.countdownshow_lb.setText('')
        else:
            self.countdown_sec -= 1
        return

    def menu_exit(self,qaction):
        print(qaction.text())   # 这样访问菜单的字面
        self.countdown_timer.stop()
        self.alarm_timer.stop()
        self.clock_timer.stop()
        self.close()
        return


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    window= MainWindow()
    window.show()
    app.exec_()
